﻿using DevExpress.Spreadsheet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using COMOSOFT.Class;
using DevExpress.XtraGrid.Views.Grid;

namespace COMOSOFT.OrderBusiness.Forms
{
    public partial class frmPreviewCostingDetails : Form
    {
        ClassShare cls = new ClassShare("frmPreviewCostingDetails");

        GridView GV;
        DataTable dtb1,dtb2;
        List<string> ListItem = new List<string>();
        public frmPreviewCostingDetails(GridView view, DataTable db1, DataTable db2, List<string> List)
        {
            InitializeComponent();
            this.GV = view;
            this.dtb1 = db1;
            this.dtb2 = db2;
            this.ListItem = List;
        }

        #region[LoadData]
        public void LoadData()
        {
            try
            {
                MemoryStream sstream = new MemoryStream();
                DevExpress.XtraPrinting.XlsExportOptions xlsOption = new DevExpress.XtraPrinting.XlsExportOptions();
                xlsOption.TextExportMode = DevExpress.XtraPrinting.TextExportMode.Value;
                xlsOption.ShowGridLines = false;
                xlsOption.ExportMode = DevExpress.XtraPrinting.XlsExportMode.SingleFile;


                GV.ExportToXls(sstream, xlsOption);
                spreadsheet_Preview.LoadDocument(sstream, DocumentFormat.Xls);
                spreadsheet_Preview.Document.Worksheets.ActiveWorksheet.Columns.AutoFit(0, 500);
                spreadsheet_Preview.Document.Worksheets.ActiveWorksheet.Rows[15].Alignment.WrapText = true;

                for (int i = 0; i < 17; i++)
                {
                    spreadsheet_Preview.ActiveWorksheet.Rows.Insert(i, 1);
                }
                if (!File.Exists(ClassShare.lcSystem + "logo.PNG"))
                    cls.Dowload(ClassShare.ftpSystem, ClassShare.lcSystem, "logo.PNG");
                spreadsheet_Preview.ActiveWorksheet.Shapes.AddPicture(ClassShare.lcSystem + "logo.PNG", 50, 50, 800, 300, false);
                spreadsheet_Preview.ActiveWorksheet.Rows[7][5].SetValue("COSTING DETAILS");
                spreadsheet_Preview.ActiveWorksheet.Rows[7][5].Font.UnderlineType = UnderlineType.Single;
                spreadsheet_Preview.ActiveWorksheet.Rows[7][5].Font.Bold = true;
                spreadsheet_Preview.ActiveWorksheet.Rows[7][5].Font.Color = Color.Black;
                spreadsheet_Preview.ActiveWorksheet.Rows[7][5].Font.Size = 18;
                spreadsheet_Preview.ActiveWorksheet.Rows[8][5].SetValue("        ( Preview Date: " + DateTime.Now.ToShortDateString()+" )");
                spreadsheet_Preview.ActiveWorksheet.Rows[8][5].Font.Italic = true;
                spreadsheet_Preview.ActiveWorksheet.Range["E8:H8"].FillColor = Color.LightGray;
                spreadsheet_Preview.ActiveWorksheet.Name = ListItem[0].ToString();
                spreadsheet_Preview.ActiveWorksheet.Rows[10][1].SetValue("Customer: " + ListItem[0].ToString());
                spreadsheet_Preview.ActiveWorksheet.Rows[11][1].SetValue("Style No: " + ListItem[1].ToString());
                spreadsheet_Preview.ActiveWorksheet.Rows[12][1].SetValue("PO: " + ListItem[2].ToString());
                spreadsheet_Preview.ActiveWorksheet.Rows[10][5].SetValue("Issue Date: " +Convert.ToDateTime(ListItem[3]).ToShortDateString());
                spreadsheet_Preview.ActiveWorksheet.Rows[11][5].SetValue("Type: " + ListItem[4].ToString());
                spreadsheet_Preview.ActiveWorksheet.Rows[12][5].SetValue("Percent Profit: " + ListItem[5].ToString()+" %");

                spreadsheet_Preview.ActiveWorksheet.Rows[9][0].SetValue("1/Master Info: ");
                spreadsheet_Preview.ActiveWorksheet.Rows[9][0].Font.Size = 10;
                spreadsheet_Preview.ActiveWorksheet.Rows[9][0].Font.Color = Color.Red;
                spreadsheet_Preview.ActiveWorksheet.Rows[9][0].Font.Bold = true;
                spreadsheet_Preview.ActiveWorksheet.Rows[9][0].Font.Italic = true;
                spreadsheet_Preview.ActiveWorksheet.Rows[16][0].SetValue("2/Details Info: ");
                spreadsheet_Preview.ActiveWorksheet.Rows[16][0].Font.Size = 10;
                spreadsheet_Preview.ActiveWorksheet.Rows[16][0].Font.Color = Color.Red;
                spreadsheet_Preview.ActiveWorksheet.Rows[16][0].Font.Bold = true;
                spreadsheet_Preview.ActiveWorksheet.Rows[16][0].Font.Italic = true;
                spreadsheet_Preview.ActiveWorksheet.Range["B10:N14"].Font.Size = 10;
                spreadsheet_Preview.ActiveWorksheet.Range["B10:N14"].Font.Color = Color.Black;
                spreadsheet_Preview.ActiveWorksheet.Range["B10:N14"].Font.Bold = true;
                spreadsheet_Preview.ActiveWorksheet.Range["B10:N14"].Font.Italic = true;

                //ExtraPrice
                int index = GV.RowCount + 20;
                spreadsheet_Preview.ActiveWorksheet.Rows[index][0].SetValue("Remark: " + ListItem[7].ToString());
                spreadsheet_Preview.ActiveWorksheet.Rows[index+2][7].SetValue("Creator");
                spreadsheet_Preview.ActiveWorksheet.Rows[index + 5][7].SetValue(ListItem[6].ToString());
                spreadsheet_Preview.ActiveWorksheet.Rows[index + 2][7].Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;
                spreadsheet_Preview.ActiveWorksheet.Rows[index + 5][7].Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;
                int idx = index + 6;
                string rang = "A" + index + ":I" + idx;
                spreadsheet_Preview.ActiveWorksheet.Range[rang].Font.Size = 10;
                spreadsheet_Preview.ActiveWorksheet.Range[rang].Font.Color = Color.Black;
                spreadsheet_Preview.ActiveWorksheet.Range[rang].Font.Bold = true;
                spreadsheet_Preview.ActiveWorksheet.Range[rang].Font.Italic = true;                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[Event]
        private void dataNavigator1_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            try
            {
                switch (e.Button.Tag.ToString())
                {
                    case "Print":
                        spreadsheet_Preview.ShowPrintDialog();
                        break;
                    case "Save":
                        String fileName = null;
                        SaveFileDialog sfd = new SaveFileDialog();
                        sfd.Filter = "Excel 2003|*.xls|Excel 2007|*.xlsx";
                        sfd.Title = "Save an Excel File";
                        sfd.FileName = "CostingDetails" + String.Format("{0:MMMddyyyy}", DateTime.Now);
                        if (sfd.ShowDialog() == DialogResult.OK)
                        {
                            fileName = sfd.FileName;
                            spreadsheet_Preview.SaveDocument(fileName, DocumentFormat.Xls);
                            DialogResult dlr = MessageBox.Show("Open File Export", "Do you want to open it?",
                                MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                            if (dlr == DialogResult.Yes)
                            {
                                System.Diagnostics.Process.Start(fileName);
                            }
                        }
                        break;
                    case "Refresh":
                        LoadData();
                        break;
                    case "Exit":
                        this.Close();
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
    }
}
